package com.beidu.lottery.call.mq.rocket;

import com.alibaba.fastjson2.JSON;
import com.beidu.lottery.call.constant.RocketMQConstant;
import com.beidu.lottery.call.dao.ActivityBaseInfoMapper;
import com.beidu.lottery.call.dao.ActivityRuleMapper;
import com.beidu.lottery.call.mq.event.ActivityDelayCloseEvent;
import com.beidu.lottery.call.mq.event.MessageWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

import static com.beidu.lottery.call.constant.ActivityGroundingStatusEnum.ACTIVITY_EXPIRED;

/**
 * 活动延时关闭消费者
 * <p>
 * 作者：北渡
 */
@Component
@RequiredArgsConstructor
@RocketMQMessageListener(
        topic = RocketMQConstant.ACTIVITY_DELAY_CLOSE_TOPIC_KEY,
        consumerGroup = RocketMQConstant.ACTIVITY_DELAY_CLOSE_CG_KEY
)
@Slf4j(topic = "ActivityDelayCloseConsumer")
public class ActivityDelayCloseConsumer implements RocketMQListener<MessageWrapper<ActivityDelayCloseEvent>> {

    private final ActivityBaseInfoMapper activityBaseInfoMapper;

    @Override
    public void onMessage(MessageWrapper<ActivityDelayCloseEvent> messageWrapper) {
        // 开头打印日志，平常可 Debug 看任务参数，线上可报平安（比如消息是否消费，重新投递时获取参数等）
        log.info("[消费者] 延迟关闭用户已领取优惠券 - 执行消费逻辑，消息体：{}", JSON.toJSONString(messageWrapper));
        ActivityDelayCloseEvent event = messageWrapper.getMessage();

        // 修改活动状态为已过期
        Integer code = ACTIVITY_EXPIRED.getCode();
        activityBaseInfoMapper.updateActivityStatus(event.getActivityId(), code);
    }
}
